package cn.stylefeng.guns.unitl;

import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.stock.model.StockInfo;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * @author eric xu
 * @date 2021/1/21
 * @description
 */
public class SinaStockUtil {
    private final static String sinaStockUrl = "http://hq.sinajs.cn/list=";

    public static void main(String[] args) {
        getStockCodeData("002299");
    }

    public static StockInfo getStockCodeData(String stockCode) {
        if (stockCode.indexOf("60") == 0) {
            stockCode = "sh" + stockCode;
        } else {
            stockCode = "sz" + stockCode;
        }
        String content = HttpUtil.get(sinaStockUrl + stockCode);
        if (StringUtils.isNotBlank(content)) {
            String[] splitStrings = content.split("\"");
            if (!Objects.isNull(splitStrings) && splitStrings.length >= 2) {
                String stockInfoString = splitStrings[1];
                String[] stockCompleteInfo = stockInfoString.split(",");
                String yesterdayClose=stockCompleteInfo[2];
                String close=stockCompleteInfo[3];
                float priceChange=(Float.valueOf(close)-Float.valueOf(yesterdayClose))/Float.valueOf(yesterdayClose)*100;
                StockInfo stockInfo=  StockInfo.builder()
                        .stockCode(stockCode)
                        .stockName(stockCompleteInfo[0])
                        .open(stockCompleteInfo[1])
                        .yesterdayClose(stockCompleteInfo[2])
                        .priceChange(priceChange)
                        .close(stockCompleteInfo[3])
                        .high(stockCompleteInfo[4])
                        .low(stockCompleteInfo[5])
                        .buyFirst(stockCompleteInfo[11])
                        .buyFirstNumber(stockCompleteInfo[10])
                        .buySecond(stockCompleteInfo[13])
                        .buySecondNumber(stockCompleteInfo[12])
                        .buyThirdNumber(stockCompleteInfo[14])
                        .buyThird(stockCompleteInfo[15])
                        .buyFourthNumber(stockCompleteInfo[16])
                        .buyFourth(stockCompleteInfo[17])
                        .buyFiveNumber(stockCompleteInfo[18])
                        .buyFive(stockCompleteInfo[19])
                        .sellFirstNumber(stockCompleteInfo[20])
                        .sellFirst(stockCompleteInfo[21])
                        .sellSecondNumber(stockCompleteInfo[22])
                        .sellSecond(stockCompleteInfo[23])
                        .sellThirdNumber(stockCompleteInfo[24])
                        .sellThird(stockCompleteInfo[25])
                        .sellFourthNumber(stockCompleteInfo[26])
                        .sellFourth(stockCompleteInfo[27])
                        .sellFiveNumber(stockCompleteInfo[28])
                        .sellFive(stockCompleteInfo[29]).build();
               // System.out.println(JSONUtil.toJsonStr(stockInfo));

               return stockInfo;
            }
        }
        return null;
    }
}
